package io.kiki.sba.registry.common.model.multi.cluster;

import io.kiki.sba.registry.common.model.slot.Slots;
import lombok.Getter;
import lombok.NonNull;

import java.io.Serializable;


public class RemoteSlotsStatus implements Serializable {
    private static final long serialVersionUID = 1L;

    /**
     * slotsEpoch
     */
    @Getter
    private final long slotsEpoch;

    /**
     * when data.slotsEpoch meta.slotsEpoch, slotsEpochConflict=true
     */
    @Getter
    private final boolean slotsEpochConflict;

    /**
     * if slot table upgrade
     */
    @Getter
    private final boolean slotsUpgrade;

    /**
     * slot table will be null if syncOnLeader=false or SlotsUpgrade=false
     */
    private final Slots slots;

    /**
     * remote meta data
     */
    @Getter
    private final DataCenter dataCenter;

    public RemoteSlotsStatus(long slotsEpoch, boolean slotsUpgrade, boolean slotsEpochConflict, Slots slots, DataCenter dataCenter) {
        this.slotsEpoch = slotsEpoch;
        this.slotsUpgrade = slotsUpgrade;
        this.slotsEpochConflict = slotsEpochConflict;
        this.slots = slots;
        this.dataCenter = dataCenter;
    }

    public static RemoteSlotsStatus conflict(Slots slots) {
        return new RemoteSlotsStatus(slots.getEpoch(), false, true, slots, null);
    }

    public static RemoteSlotsStatus notUpgrade(long slotsEpoch, DataCenter dataCenter) {
        return new RemoteSlotsStatus(slotsEpoch, false, false, null, dataCenter);
    }

    public static RemoteSlotsStatus upgrade(@NonNull Slots slots, DataCenter dataCenter) {

        return new RemoteSlotsStatus(slots.getEpoch(), true, false, slots, dataCenter);
    }


    public Slots getSlots() {
        return slots;
    }
}
